package com.milotnt.controller;

import com.milotnt.enums.StaffEnum;
import com.milotnt.pojo.EmployeeEntity;
import com.milotnt.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Slf4j
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //查询员工
    @RequestMapping("/selEmployee")
    public String selectEmployee(Model model) {
        List<EmployeeEntity> employeeList = employeeService.findAll();
        model.addAttribute("employeeList", employeeList);
        return "selectEmployee";
    }

    //跳转新增员工页面
    @RequestMapping("/toAddEmployee")
    public String toAddEmployee() {
        return "addEmployee";
    }

    //新增员工
    @RequestMapping("/addEmployee")
    public String addEmployee(EmployeeEntity employee) {
        //工号随机生成
        Random random = new Random();
        String account1 = "2025";
        for (int i = 0; i < 8; i++) {
            account1 += random.nextInt(10);
        }
        long account = 0;
        try {
            // 转换为Long包装类
            Long numObj = Long.valueOf(account1);
            // 自动拆箱为long基本类型
            account = numObj;
        } catch (NumberFormatException e) {
            System.out.println("转换失败：" + e.getMessage());
        }

        //获取当前日期
        Date nowDay = new Date();

        employee.setEmployeeAccount(account);
        employee.setEntryTime(nowDay);

        employeeService.insertEmployee(employee);

        return "redirect:selEmployee";

    }

    //删除员工
    @RequestMapping("/delEmployee")
    public String deleteEmployee(Long employeeAccount) {
        employeeService.deleteByEmployeeAccount(employeeAccount);
        return "redirect:selEmployee";
    }

    //跳转员工修改页面
    @RequestMapping("/toUpdateEmployee")
    public String toUpdateEmployee(Long employeeAccount, Model model) {
        List<EmployeeEntity> employeeList = employeeService.selectByEmployeeAccount(employeeAccount);
        model.addAttribute("employeeList", employeeList);
        return "updateEmployee";
    }

    //修改员工信息
    @RequestMapping("/updateEmployee")
    public String updateEmployee(EmployeeEntity employee) {
        employeeService.updateMemberByEmployeeAccount(employee);
        return "redirect:selEmployee";
    }

    //获取所有教练
    @GetMapping("/getCoaches")
    @ResponseBody
    public Map<String, Object> getCoaches() {

        Map<String, Object> result = new HashMap<>();

        List<EmployeeEntity> coaches = new ArrayList<>();
        coaches.addAll(employeeService.selectByStaff(StaffEnum.健身教练.getDescription()));
        coaches.addAll(employeeService.selectByStaff(StaffEnum.实习教练.getDescription()));

        result.put("coaches", coaches);

        return result;
    }
}
